package com.wasin.server.controller;

import com.diboot.core.vo.JsonResult;
import com.wasin.server.quarter.TaskJob;
import com.wasin.server.quarter.TaskScheduler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ 作者： wasin
 * @ 创建时间： 2021/9/26  11:07
 * @ 说明：
 * @ 当前版本:  1.0.0
 */
@RestController
@Api(tags = "定时任务测试")
public class QuarterController {

    @Autowired
    TaskScheduler taskScheduler;

    @ApiOperation("添加定时任务")
    @PostMapping(value = "/createTaskQuarter")
    public JsonResult createTaskQuarter(String cron,String taskId,String content) throws SchedulerException {
        taskScheduler.setTaskExecuteDate(cron,taskId,content,TaskJob.class);
        return JsonResult.OK();
    }
    @ApiOperation("删除定时任务")
    @PostMapping(value = "/delTaskQuarter")
    public JsonResult delTaskQuarter(String taskId) throws SchedulerException {
        taskScheduler.delJob(taskId);
        return JsonResult.OK();
    }

    @ApiOperation("更新定时任务")
    @PostMapping(value = "/updateTaskQuarter")
    public JsonResult updateTaskQuarter(String cron,String taskId,String content) throws SchedulerException {
        taskScheduler.updateJob(cron,taskId,content);
        return JsonResult.OK();
    }

    @ApiOperation("暂停定时任务")
    @PostMapping(value = "/pauseTaskQuarter")
    public JsonResult pauseTaskQuarter(String taskId) throws SchedulerException {
        taskScheduler.pauseJob(taskId);
        return JsonResult.OK();
    }
    @ApiOperation("恢复定时任务")
    @PostMapping(value = "/resumeTaskQuarter")
    public JsonResult resumeTaskQuarter(String taskId) throws SchedulerException {
        taskScheduler.resumeJob(taskId);
        return JsonResult.OK();
    }

    /**
     * BLOCKED  阻塞
     * COMPLETE  完成
     * ERROR  错误
     * NONE  不存在
     * NORMAL 正常
     * PAUSED 暂停
     * @param taskId
     * @return BLOCKED  阻塞,COMPLETE  完成, ERROR  错误, NONE  不存在, NORMAL 正常, PAUSED 暂停
     * @throws SchedulerException
     */
    @ApiOperation("获取当前定时任务的执行状态")
    @PostMapping(value = "/getTaskQuarterState")
    public JsonResult getTaskQuarterState(String taskId) throws SchedulerException {
        Trigger.TriggerState jobState = taskScheduler.getJobState(taskId);
        return JsonResult.OK(TaskScheduler.transferredState(jobState));
    }


}
